#include <all_header.h>
static QueueHandle_t que;
static uint8_t buffer[20];

void tip_task_init(void) {
	que = xQueueCreate(5, 1);
	ASSERT(que);
}

void tip_task_run(void *arg) {
	uint8_t data;
	while (true) {
		ASSERT(xQueueReceive(que,&data,portMAX_DELAY) == pdTRUE);
		switch ((tip_task_state_t) data) {
		case TIP_TASK_STATE_STARTUP:
			LOG_SYNC("bootloader version %d.%d %s %s" NL, VERSION, VERSION_SUB,
					__DATE__, __TIME__);
			break;
		case TIP_TASK_STATE_APP_FLAG_WRONG:
			LOG_D("app update flag is wrong 0x%02x" NL,
					flash_data_task_get_flag());
			break;
		case TIP_TASK_STATE_SHOW_CHOICES:
			LOG_D(" choices" NL
			/**/"1.update app" NL
			/**/"2.update uboot" NL
			/**/"3.reset" NL);
			break;
		case TIP_TASK_STATE_DOWN_APP:
			LOG_D("wait app file..." NL);
			break;
		case TIP_TASK_STATE_DOWN_BOOTLOADER:
			LOG_D("wait bootloader file..." NL);
			break;
		case TIP_TASK_STATE_DOWNING_BOOTLOADER:
		case TIP_TASK_STATE_DOWNING_APP:
			break;
		case TIP_TASK_STATE_PREPARE_APP:
			LOG_D("app download prepare ..." NL);
			break;
		case TIP_TASK_STATE_PREPARE_BOOTLOADER:
			LOG_D("bootloader download prepare ..." NL);
			break;
		case TIP_TASK_STATE_APP_SIZE_TOO_BIG:
			LOG_D("app size is too big" NL);
			break;
		case TIP_TASK_STATE_BOOTLOADER_SIZE_TOO_BIG:
			LOG_D("bootloader size too big" NL);
			break;
		case TIP_TASK_STATE_APP_SIZE_TOO_SMALL:
			LOG_D("app size is too small" NL);
			break;
		case TIP_TASK_STATE_BOOTLOADER_SIZE_TOO_SMALL:
			LOG_D("bootloader size too small" NL);
			break;
		case TIP_TASK_STATE_DOWN_APP_OK:
			LOG_D("download app success" NL);
			break;
		case TIP_TASK_STATE_DOWN_BOOTLOADER_OK:
			LOG_D("download bootloader success" NL);
			break;
		case TIP_TASK_STATE_UPDATE_APP:
			LOG_D("start update app" NL);
			break;
		case TIP_TASK_STATE_UPDATE_BOOTLOADER:
			LOG_D("start update bootloader" NL);
			break;
		case TIP_TASK_STATE_UPDATE_APP_SUCCESS:
			LOG_D("update app success" NL);
			break;
		case TIP_TASK_STATE_UPDATE_BOOTLOADER_SUCCESS:
			LOG_D("update bootloader success" NL);
			break;
		case TIP_TASK_STATE_UPDATE_CHECK_ERR:
			LOG_D("application check error %s" NL, buffer);
			break;
		case TIP_TASK_STATE_FLASH_COPY_WRONG:
			LOG_D("flash copy wrong" NL);
			break;
		}
	}
}

void tip_task_set_state(tip_task_state_t state) {
	uint8_t data = (uint8_t) state;
	ASSERT(xQueueSend(que, &data, DEFAULT_WAIT_TICKS) == pdTRUE);
}

void tip_task_set_info(uint8_t *data, uint32_t len) {
	memset(buffer, 0, sizeof(buffer));
	memcpy(buffer, data, len);
}

void tip_task_set_info_no(int err_number) {
	char buf[10];
	snprintf(buf, sizeof(buf), "%d", err_number);
	tip_task_set_info(buf, strlen(buf));
}
